﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace TicketMovie.Admin
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private DataClasses1DataContext dc = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.DropDownList1.DataSource = this.dc.Categories.ToList();
                DropDownList1.DataValueField = "code_category";
                DropDownList1.DataTextField = "name_category";
                DropDownList1.DataBind();
                this.DropDownList2.DataSource = this.dc.Countries.ToList();
                DropDownList2.DataValueField = "code_country";
                DropDownList2.DataTextField = "name_country";
                DropDownList2.DataBind();
            }
        }
        protected void Button_Click(object sender, EventArgs e)
        {
            try
            {
                Film addfilm = new Film();
                addfilm.film_name = this.filmname.Value;
                //this.DropDownList1.DataSource = this.dc.Films.Where(addcata => addcata.Category.Equals("code_category"));
                //this.DropDownList1.DataBind();
                //this.DropDownList2.DataSource = this.dc.Films.Where(addcountry => addcountry.Country.Equals("code_country"));
                //this.DropDownList2.DataBind();
                addfilm.code_category = Convert.ToInt32(this.DropDownList1.SelectedValue);
                addfilm.code_country = Convert.ToInt32(this.DropDownList2.SelectedValue);

                addfilm.length_hours = Convert.ToInt32(this.length_hour.Value);
                addfilm.length_minutes = Convert.ToInt32(this.length_minute.Value);
                addfilm.started = Convert.ToDateTime(this.start.Value);
                addfilm.the_end = Convert.ToDateTime(this.end.Value);

                //upload images
                double size = (double)this.FileUpload1.FileBytes.Count() / 1024 / 1024;
                string filenamebefore = this.FileUpload1.FileName;
                if (!filenamebefore.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) && !filenamebefore.EndsWith("gif", StringComparison.OrdinalIgnoreCase) && !filenamebefore.EndsWith("png", StringComparison.OrdinalIgnoreCase))
                    this.Label1.Text = "Invalid file format!";
                else
                {
                    if (size > 1)
                        this.Label1.Text = "File size too large!!";
                    else
                    {
                        string filenames = DateTime.Now.ToString("yyyyMMddHHmmss") + this.FileUpload1.FileName;
                        this.FileUpload1.SaveAs(Server.MapPath("img/posters/" + filenames));
                        addfilm.image = filenames;
                        
                    }
                }    
                
                            
                addfilm.notes = this.note.Value;
                addfilm.detail = this.Detail.Value;
                addfilm.price = Convert.ToUInt32(this.Price.Value);
                addfilm.trailer = this.Trailer.Value;
                this.dc.Films.InsertOnSubmit(addfilm);
                this.dc.SubmitChanges();
                this.message_error.Visible = false;
                if (this.message_error.Visible == false)
                {
                    Response.Redirect("MoviesList.aspx");
                }
            }
            catch
            {
                this.message_error.Visible = true;
            }
        }
    }
}